//
//  modifyNameViewController.m
//  IMChat
//
//  Created by Admin on 2021/6/28.
//  Copyright © 2021年 TeamOne. All rights reserved.
//

#import "modifyNameViewController.h"

@interface modifyNameViewController ()

//返回按钮
- (IBAction)cancel:(id)sender;

//新昵称(property的命名使用了cocoa的命名约定，也就是property的命名开头使用了保留字new、copy等，如copyString、newString等。)
@property (weak, nonatomic) IBOutlet UITextField *textName;

//确认修改按钮属性
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;


//确认修改按钮点击
- (IBAction)sureBtnClick:(UIButton *)sender;



@end

@implementation modifyNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //监听新昵称内容
    [self.textName addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:(UIControlEventEditingChanged)];
    
    /** 获得全局变量. */
    AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    self.textName.text = myDelegate.user.userName;
    
}

//取消按钮点击
- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

//搜索框有内容时登录按钮才能用
- (void)textFieldValueChanged:(UITextField *)textField {
    NSLog(@"你输入了%@", textField.text);
    self.sureBtn.enabled = self.textName.text.length && self.textName.text.length ;
}

//点击空白处回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

//确认修改按钮点击
- (IBAction)sureBtnClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:true];
    NSLog(@"你点击了确认修改按钮！");
    NSString *nameNew = self.textName.text;
    
    /** 获得全局变量. */
    AppDelegate *myDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    myDelegate.user.userName = nameNew;
    [self alertTool:@"昵称修改成功！"];
    
    [TOAFHttpUtils updateUserById:myDelegate.user];
    
    nameNew = [nameNew stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([nameNew isEqualToString:@""]) {
        //如果未输入或者输入的是空格弹出警示
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"不能为空！" message:nil preferredStyle:UIAlertControllerStyleAlert];//先创建一个弹窗控制器 然后里面有title和message来提示需要的东西
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];//添加一个弹窗的action 活动可以是取消 可以是确认
        [alert addAction:sureAction];
        [self presentViewController:alert animated:NO completion:nil];
        
        self.textName.text = @"";
        [self.textName becomeFirstResponder];
        return;
    }
    
}

// 弹窗工具
- (void)alertTool:(NSString *)message{
    //先创建一个弹窗控制器 然后里面有title和message来提示需要的东西
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
    //添加一个弹窗的action 活动可以是取消 可以是确认
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:sureAction];
    [self presentViewController:alert animated:NO completion:nil];
}

@end
